package com.village.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Transportation;
import com.village.zpark.service.TransportationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/transportation")
public class TransportationController {
    @Autowired
    private TransportationService transportationService;




    @RequestMapping
    public String Transportation(){
        return "/transportation";
    }

    @RequestMapping(value = "/transportationPage")
    public String TransportationPage(){
        return "/transportationPage";
    }

    @RequestMapping(value = "/transportationQueryById")
    public Object TransportationQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Transportation transportation = transportationService.findById(id);
        model.addAttribute("obj",transportation);
        return "/transportationPage";
    }

    @RequestMapping(value = "/transportationList")
    @ResponseBody
    public Object TransportationList(){
        List<Transportation> transportationList = transportationService.findAll();
        return ResultMapUtil.getHashMapList(transportationList);
    }


    @RequestMapping(value = "/addtransportation")
    @ResponseBody
    public Object addTransportation(Transportation transportation){
        try {
            int i = transportationService.addTransportation(transportation);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/updatetransportation")
    @ResponseBody
    public Object updateTransportation(Transportation transportation){
        try {
            int i = transportationService.updateTransportation(transportation);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/deletetransportation")
    @ResponseBody
    public Object deleteTransportation(Integer id){
        try {
            int i = transportationService.deleteTransportationById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/transportationQueryPage")
    @ResponseBody
    public Map<String, Object> TransportationQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Transportation> transportationPageInfo = transportationService.findPage(page, limit, param);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", transportationPageInfo.getTotal());
        map.put("data", transportationPageInfo.getList());
        return map;
    }
}
